package jike.demo16.code02;

import jike.demo16.AlertRule;
import jike.demo16.Notification;

/**
 * @param
 * @author oyz
 * @version 1.0.4
 * @description: 现在，如果我们需要添加一个功能，当每秒钟接口超时请求个数，超过某个预先设置的最大
 * 阈值时，我们也要触发告警发送通知
 * @date 2022/11/8 23:08
 */
public class Alert {
    private static final String URGENCY = "系统很紧急";

    private AlertRule rule;

    private Notification notification;

    public Alert(AlertRule rule, Notification notification) {
        this.rule = rule;
        this.notification = notification;
    }

    // 改动一:入参加入超时时间
    public void check(String api,long requestCount,long errorCount, long durationOfSeconds,long timeOut){
        long tps = requestCount / durationOfSeconds;
        if (tps > rule.getMatchedRule(api).getMaxTps()) {
            notification.notify(URGENCY, "准备报警。。。");
        }

        if (errorCount > rule.getMatchedRule(api).getMaxErrorCount()) {
            notification.notify(URGENCY, "准备报警。。。");
        }
        // 改动二:增加判断逻辑
        if (timeOut > rule.getMatchedRule(api).getTimeOut()) {
            notification.notify(URGENCY, "准备报警。。。");
        }

    }

}
